package com.test.platform.ui.screen

import XTSReportRepository
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

// 添加必要的导入
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.lifecycle.viewmodel.compose.viewModel
import com.test.platform.viewmodel.XTSReportViewModel
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.TextButton
import com.test.platform.model.XTSReport

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun XTSReportScreen(
    modifier: Modifier = Modifier,
    viewModel: XTSReportViewModel = viewModel { 
        XTSReportViewModel(XTSReportRepository())
    }
) {
    val reports by viewModel.reports.collectAsState()
    val loading by viewModel.loading.collectAsState()
    var selectedTab by remember { mutableStateOf(0) }
    
    LaunchedEffect(Unit) {
        viewModel.loadReports()
    }

    Scaffold(
        modifier = modifier,
        topBar = {
            Column {
                TopAppBar(
                    title = { Text("XTS报告") }
                )
                TabRow(selectedTabIndex = selectedTab) {
                    Tab(
                        selected = selectedTab == 0,
                        onClick = { selectedTab = 0 },
                        text = { Text("运行状态") }
                    )
                    Tab(
                        selected = selectedTab == 1,
                        onClick = { selectedTab = 1 },
                        text = { Text("历史报告") }
                    )
                }
            }
        }
    ) { paddingValues ->
        if (loading) {
            CircularProgressIndicator(
                modifier = Modifier
                    .fillMaxSize()
                    .wrapContentSize()
            )
        } else {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues)
                    .padding(16.dp)
            ) {
                when (selectedTab) {
                    0 -> RunningStatusContent(viewModel)
                    1 -> ReportHistoryContent(reports)
                }
            }
        }
    }
}

@Composable
private fun RunningStatusContent(viewModel: XTSReportViewModel) {
    var progress by remember { mutableStateOf(0.75f) }
    var isRunning by remember { mutableStateOf(true) }
    
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "当前状态：${if (isRunning) "运行中" else "已停止"}",
                style = MaterialTheme.typography.titleMedium
            )
            Spacer(modifier = Modifier.height(16.dp))
            LinearProgressIndicator(
                progress = progress,
                modifier = Modifier.fillMaxWidth()
            )
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text("已完成: ${(progress * 100).toInt()}%")
                Text("剩余时间: 30分钟")
            }
            
            Spacer(modifier = Modifier.height(16.dp))
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                Button(
                    onClick = { isRunning = !isRunning }
                ) {
                    Text(if (isRunning) "暂停" else "继续")
                }
                Button(
                    onClick = { /* TODO */ },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error
                    )
                ) {
                    Text("停止")
                }
            }
        }
    }
}

@Composable
private fun ReportHistoryContent(reports: List<XTSReport>) {
    LazyColumn {
        items(reports) { report ->
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                ) {
                    Text("✅ 测试结果: ${report.runningResult}")
                    Text("❌ 失败原因: ${report.failReason ?: "无"}")
                    Text("📄 报告地址: ${report.reportAddress}")
                    Text("API版本: ${report.apiVersion}")
                    Text("迭代版本: ${report.iterationVersion}")
                    
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 8.dp),
                        horizontalArrangement = Arrangement.End
                    ) {
                        TextButton(
                            onClick = { /* TODO: 查看详情 */ }
                        ) {
                            Text("查看详情")
                        }
                    }
                }
            }
        }
    }
}